package com.pizza.database.mapping;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.pizza.database.DBConnection;
import com.pizza.model.Article;

/**
 * Class ArticleDB, mapping avec la bdd.
 *
 */
public class ArticleDB implements Serializable {

	private DBConnection dbConnexion;

	public ArticleDB(DBConnection _dbConnexion) {
		dbConnexion = _dbConnexion;
	}

	public Article getArticleById(int _id) {
		Connection connexion = dbConnexion.getConnection();
		Article retour = null;
		StringBuilder query = new StringBuilder();
		query.append("SELECT *");
		query.append(" FROM articles");
		query.append(" WHERE id=?");
		try {
			PreparedStatement pStatement = connexion.prepareStatement(query
					.toString());
			pStatement.setInt(1, _id);
			ResultSet rs = pStatement.executeQuery();
			rs.first();

			String nom = rs.getString("nom");
			float prix = rs.getFloat("prix");
			String description = rs.getString("description");
			String url_image = rs.getString("url_image");
			String type = rs.getString("type");

			retour = new Article(_id, nom, prix, description, url_image, type);

			pStatement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return retour;
	}
}
